package ui.component
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import flashx.textLayout.formats.TextAlign;
	
	public class ProgressBar extends Sprite
	{
		private var _title:String = "正在下载资源";
		private var _titleText:TextField;
		private var _value:uint = 0;
		private var _valueBitmap:Bitmap;
		private var _text:TextField;
		private var _progress:Bitmap;
		private var _width:uint=400;
		private var _height:uint=20;
		private var _color:uint = 0xdddddd;
		
		private var _timer:Timer = new Timer(500);
		private var _cot:int = 0;
		
		public function get value():uint
		{
			return _value;
		}

		public function set value(value:uint):void
		{
			_value = value;
			_text.text = String(_value)+"% ";
			_valueBitmap.scaleX = value*_width*0.01;
			
			if(value==100)_timer.stop();
		}
		
		public function ProgressBar(Width:uint)
		{
			_width = Width;
			
			_titleText = new TextField;
			_titleText.textColor = _color;
			_titleText.width = 200;
			var format:TextFormat = new TextFormat("Microsoft YaHei");
			format.align = TextAlign.LEFT;
			format.size = _height*0.8;
			_titleText.defaultTextFormat = format;
			_titleText.text = _title;
			_titleText.y = -_height*1.2;
			addChild(_titleText);
			
			_text = new TextField;
			_text.x = _width-46;
			_text.textColor = _color;
			_text.mouseEnabled = false;
			format = new TextFormat("Microsoft YaHei");
			format.align = TextAlign.RIGHT;
			format.size = _height-2;
			format.italic = true;
			_text.defaultTextFormat = format;
			addChild(_text);
			
			var bmd:BitmapData = new BitmapData(_width,_height,false,_color);
			var bmdempty:BitmapData = new BitmapData(_width-2,_height-2,false,0x222222);
			bmd.copyPixels(bmdempty,bmdempty.rect,new Point(1,1));
			_progress = new Bitmap(bmd);
			addChild(_progress);
			
			var _valBmd:BitmapData = new BitmapData(1,_height-2,false,_color);
			_valueBitmap = new Bitmap(_valBmd);
			_valueBitmap.x = _valueBitmap.y = 1;
			addChild(_valueBitmap);
			
			value = 0;
			
			addEventListener(Event.ADDED_TO_STAGE,onAdded);
			addEventListener(Event.REMOVED,onRemove);
		}
		
		private function onTimer(e:TimerEvent):void
		{
			_cot++;
			if(_cot>3)_cot=0;
			
			var str:String = "";
			for(var i:int=0;i<_cot;i++)str+=". "
			
			_titleText.text = (_title+str);	
		}

		private function onResize(e:Event):void
		{
			x = (Global.stage.stageWidth-width)*0.5;
			y = (Global.stage.stageHeight-height);
		}
		private function onAdded(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onAdded);
			
			x = (Global.stage.stageWidth-width)*0.5;
			y = (Global.stage.stageHeight-height);
			Global.stage.addEventListener(Event.RESIZE,onResize);
			
			_timer.start();
			_timer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		private function onRemove(e:Event):void
		{
			Global.stage.removeEventListener(Event.RESIZE,onResize);
			
			_timer.stop();
			_timer.removeEventListener(TimerEvent.TIMER,onTimer);
		}

	}
}